/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "UUser.getById", query = "SELECT u FROM UUser u WHERE u.id=:id"),
    @NamedQuery(name = "UUser.getByEmail", query = "SELECT u FROM UUser u WHERE u.email=:email"),
    @NamedQuery(name = "UUser.login", query = "SELECT u FROM UUser u WHERE u.email=:email AND u.password=:password"),
    @NamedQuery(name = "UUser.getLike", query = "SELECT u FROM UUser u WHERE u.id<>:id AND (UPPER(CONCAT(u.lastname, ' ', u.firstname)) LIKE UPPER(:str) OR UPPER(CONCAT(u.firstname, ' ', u.lastname)) LIKE UPPER(:str)) ORDER BY u.lastname")})
public class UUser implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String avatar;
    private String email;
    private String firstname;
    private String lastname;
    private String password;
    @OneToMany(fetch= FetchType.EAGER)
    private Collection<Playlist> playlists;
    @OneToMany
    private Collection<Music> favoriteMusics;
    @OneToMany
    private Collection<Album> favoriteAlbums;
    @OneToMany
    private Collection<Music> lastPlayedMusics;
    // Friendship system
    //
    @OneToMany
    private Collection<UUser> friends;
    @OneToMany
    private Collection<Notification> notifications;
    @ManyToMany(mappedBy = "sharedUsers")
    private List<Playlist> sharedPlaylists;

    //
    // Constructors
    //
    public UUser() {
    }

    public UUser(String avatar, String email, String firstname, String lastname, String password) {
        this.avatar = avatar;
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        
        this.playlists = new ArrayList<Playlist>();
        this.favoriteMusics = new ArrayList<Music>();
        this.favoriteAlbums = new ArrayList<Album>();
        this.lastPlayedMusics = new ArrayList<Music>();
        
        this.friends = new ArrayList<UUser>();
        this.notifications = new ArrayList<Notification>();
        this.sharedPlaylists = new ArrayList<Playlist>(); // Shared playlists whiches the user isn't the owner
    }
    
    //
    // Getters / Setters
    //
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Collection<Playlist> getPlaylist() {
        return playlists;
    }

    public void setPlaylist(Collection<Playlist> playlists) {
        this.playlists = playlists;
    }

    public Collection<Music> getFavoriteMusics() {
        return favoriteMusics;
    }

    public void setFavoriteMusics(Collection<Music> favoriteMusics) {
        this.favoriteMusics = favoriteMusics;
    }

    public Collection<Album> getFavoriteAlbums() {
        return favoriteAlbums;
    }

    public void setFavoriteAlbums(Collection<Album> favoriteAlbums) {
        this.favoriteAlbums = favoriteAlbums;
    }

    public Collection<Music> getLastPlayedMusics() {
        return lastPlayedMusics;
    }

    public void setLastPlayedMusics(Collection<Music> lastPlayedMusics) {
        this.lastPlayedMusics = lastPlayedMusics;
    }

    public Collection<Playlist> getPlaylists() {
        return playlists;
    }

    public void setPlaylists(Collection<Playlist> playlists) {
        this.playlists = playlists;
    }

    public Collection<UUser> getFriends() {
        return friends;
    }

    public void setFriends(Collection<UUser> friends) {
        this.friends = friends;
    }

    public Collection<Notification> getNotifications() {
        return notifications;
    }

    public void setNotifications(Collection<Notification> notifications) {
        this.notifications = notifications;
    }

    public List<Playlist> getSharedPlaylists() {
        return sharedPlaylists;
    }

    public void setSharedPlaylists(List<Playlist> sharedPlaylists) {
        this.sharedPlaylists = sharedPlaylists;
    }
    
    public boolean addPlaylist(Playlist playlist) {
        return playlists.add(playlist);
    }
    
    public boolean removePlaylist(Playlist playlist) {
        return playlists.remove(playlist);
    }
    
    public boolean addFriend(UUser friend) {
        if(!friends.contains(friend)) {
            return friends.add(friend);
        }
        else {
            return false;
        }
    }
    
    public boolean isFriendWith(UUser user) {
        return friends.contains(user);
    }
    
    public boolean removeFriend(UUser friend) {
        return friends.remove(friend);
    }
    
    public boolean addNotification(Notification notif) {
        return notifications.add(notif);
    }
    
    public boolean addSharedPlaylist(Playlist playlist) {
        if(!sharedPlaylists.contains(playlist)) {
            return sharedPlaylists.add(playlist);
        }
        
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UUser)) {
            return false;
        }
        UUser other = (UUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.UUser[ id=" + id + " ]";
    }
}
